package cn.itcast.p2p20.fragments;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;

/**
 * A simple {@link Fragment} subclass.
 * <p/>
 * FragmentPagerAdapter   在调用instantiateItem 在调用fragment = getItem(position);返回了Fragment会将返回定位Fragment缓存到FragmentManager
 * 下次不管调用instantiateItem还是destroyItem， 都直接从FragmentManager缓存拿Fragment
 * D/InvestFragment: instantiateItem() called
 * D/[产品列表]: onCreateView() called
 * D/[产品列表]: onActivityCreated() called
 * D/[产品列表]: onStart() called
 * D/[产品列表]: onResume() called
 * <p/>
 * D/InvestFragment: destroyItem() called
 * D/[产品推荐]: onPause() called
 * D/[产品推荐]: onStop() called
 * D/[产品推荐]: onDestroyView() called
 * <p/>
 * FragmentStatePagerAdapter 调用instantiateItem在调用fragment = getItem(position);不会缓存Fragment,每次调用instantiateItem在调用的时候都会调用fragment = getItem(position)
 * <p/>
 * D/[产品推荐]: onPause() called
 * D/[产品推荐]: onStop() called
 * D/[产品推荐]: onDestroyView() called
 * D/[产品推荐]: onDestroy() called
 * D/[产品推荐]: onDetach() called
 * D/InvestFragment: instantiateItem() called
 * D/InvestFragment: getItem() called : 2
 * <p/>
 * D/[产品推荐]: onAttach() called
 * D/[产品推荐]: onCreate() called
 * D/[产品推荐]: onCreateView() called
 * D/[产品推荐]: onActivityCreated() called
 * D/[产品推荐]: onStart() called
 * D/[产品推荐]: onResume() called
 * <p/>
 * <p/>
 * FragmentManager:moveToState--->Fragment:restoreViewState--->restoreHierarchyState--->dispatchRestoreInstanceState--->onRestoreInstanceState----->setCurrentItemInternal
 */
public class InvestFragment extends BaseFragment {

    private static final String TAG = "InvestFragment";
    private List<Fragment> mFragments = new ArrayList<>();//Arrays.asList(SimpleFragment.createSimpleFragment(TITLE[0]), SimpleFragment.createSimpleFragment(TITLE[1]), SimpleFragment.createSimpleFragment(TITLE[2]));

    {
        mFragments.add(new AllProductListFragment());
        mFragments.add(new HotProductFragment());
        mFragments.add(new ProductRecommentFragment());

    }

    public static final String TITLE[] = {"[产品列表]", "[热门产品]", "[产品推荐]"};
    @InjectView(R.id.tabpageindicator)
    TabPageIndicator tabpageindicator;
    @InjectView(R.id.viewpager)
    ViewPager viewpager;

    //内部嵌套了Fragment切换的时候需要重新inflater.inflate去触发内部Fragment的生命周期
    @Override
    protected boolean needReUseRootView() {
        return false;
    }

    @Override
    protected void initData() {

        showSuccessStateLayout();
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);

        //viewpager.setAdapter(new MyFragmentPagerAdapter(getFragmentManager()));

        viewpager.setAdapter(new MyFragmentPagerAdapter(/*getFragmentManager()*/getChildFragmentManager()));

        int currentItem = viewpager.getCurrentItem();
        //选中0， 说明后来又修改了
        Log.d("ViewPager", "initView() currentItem:" + currentItem);

        viewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {

                Log.d("ViewPager", "position = [" + position + "]");
                super.onPageSelected(position);
            }
        });




        /*viewpager.setAdapter(new PagerAdapter() {

            @Override
            public CharSequence getPageTitle(int position) {
                return TITLE[position];
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                TextView textView = new TextView(mContext);
                textView.setGravity(Gravity.CENTER);
                textView.setText(TITLE[position]);
                container.addView(textView);
                return textView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {

                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return TITLE.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });*/

        tabpageindicator.setViewPager(viewpager);
    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position];
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Log.d(TAG, "instantiateItem() called");
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Log.d(TAG, "destroyItem() called");
            super.destroyItem(container, position, object);
        }

        @Override
        public Fragment getItem(int position) {

            Log.d(TAG, "getItem() position:" + position);
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    private class MyMyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

        public MyMyFragmentStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Log.d(TAG, "instantiateItem() called");
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            Log.d(TAG, "destroyItem() called");
            super.destroyItem(container, position, object);
        }

        @Override
        public Fragment getItem(int position) {
            Log.d(TAG, "getItem() called : " + position);
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position];
        }

    }


    @Override
    protected String getTitle() {
        return "投资";
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_invest;
    }

}
